<style>
.outer{width: 100%;height: 100%;overflow-y: auto;position: relative;}
.add_button{position: absolute;top: 0px;right: 10px;}
</style>
<template>
  <div class="outer" style="">
    <config_group_loop :group_map="group_map" v-model="on_full_name" @change_tab="change_tab" @change_value="init">

    </config_group_loop>

    <el-button class="add_button" v-if="in_right_admin('ConfigGroup/add')" type="primary" @click="iframe('config_group/edit')">添加配置组</el-button>
  </div>
</template>
<script>
import config_group_loop from '@/components/config_group_loop.vue'
import {iframe, in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
export default {
  components: {config_group_loop},
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('ConfigGroup/add')) btns['添加'] = function(){ iframe('config_group/edit') }

    if (is_del){
      if(in_right_admin('ConfigGroup/restore')) btns['恢复'] = function(){ app.$refs['list_url'].restore() }
      if(in_right_admin('ConfigGroup/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }
    }else {

      if(in_right_admin('ConfigGroup/del')) btns['删除'] = function(){ app.$refs['list_url'].del() }
      if(in_right_admin('ConfigGroup/recycle')) btns['回收站'] = function(){ iframe('config_group/index?is_del=1') }
    }
    let forms_config = {
      create_time: {label:'创建时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      head_img: {name:'head_img', label:'头图', width:'60px', type:'file_img'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      group_map: [],
      on_full_name: '',
      config: {
        api: '/admin/_base_/'+(is_del?'recycle':'list'),
        title: "__列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    iframe: iframe,
    in_right_admin: in_right_admin,
    init(){
      let data = {parent_id: 0}
      let on_full_name = this.$get('full_name',s94Mjs.cache('on_full_name', ''));
      if (on_full_name) data.on_full_name = on_full_name;
      ajax({url:'/admin/config_group/config_map', data:data}, (res)=>{
        this.group_map = res.data['group_map'];
        return false
      })
    },
    change_tab(){
      // 缓存on_full_name
      let arr = [];
      s94Mjs.eachloop(this.group_map, 'children', (row)=>{
        if (row.on===1){
          arr.push(row.name);
          return true;
        }
      })
      s94Mjs.cache({on_full_name: arr.join('.')});
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    this.init();
  }
}
</script>
